<template>
    <view class="container">
  
      <view class="avatar">
        <!-- 上传组件 -->
        <u-upload
          name="product_img"
          :maxCount="1"
          :fileList="filelist"
          width="100"
          height="100"
        
        />
      </view>
      <view class="content">
        <u-form
          labelPosition="left"
          labelWidth="60"
          :model="buslevel"
          :rules="rules"
          ref="product"
        >
          <!-- 名称 -->
          <u-form-item label="会员昵称" prop="nickname">
            <u-input v-model="buslevel.nickname" disabled placeholder="会员昵称未知" />
          </u-form-item>
          <u-form-item label="会员电话">
            <u-input v-model="buslevel.mobile" disabled placeholder="会员电话未知" />
          </u-form-item>
  
          <!-- 商品描述 -->
          <u-form-item label="等级" borderBottom
          @click="showlevel = true"
  >
            <u-input
            
              v-model="levelname_text"
              placeholder="会员等级未知"
              border="none"
              disabled
              disabledColor="#ffffff"
  
  
            />
            <u-icon slot="right" name="arrow-right"></u-icon>
  
          </u-form-item>
          <u-form-item  @click="show = true" label="有效期">
            <u-input
              border="none"
              disabled
              disabledColor="#ffffff"
              v-model="expdate_text"
              placeholder="会员有效期未知"
            />
            <u-icon slot="right" name="arrow-right"></u-icon>
  
          </u-form-item>
          <u-form-item label="余额" >
            <u-input
              
              v-model="buslevel.money"
              placeholder="会员余额未知"
              disabled
            />
          </u-form-item>
  
          <u-form-item label="创建时间" >
            <u-input v-model="buslevel.createtime_text" disabled         placeholder="创建时间未知"
        />
          </u-form-item>
  
          <u-action-sheet
            :show="showlevel"
            :actions="levellist"
            title="请选择会员套餐等级"
            @close="showlevel = false"
            @select="levelSelect()"
          />
  
          <u-datetime-picker
                  :show="show"
                  v-model="value1"
                  mode="datetime"
                  @confirm = confirm	
                  @cancel = cancel
  
          />


  
       
          <!-- 提交按钮 -->
          <u-button
            text="赋予会员"
            icon="pushpin"
            size="small"
            type="success"
            :customStyle="btnStyle"
            @click="product"
          />
        </u-form>
      </view>
      <!-- 消息提醒 -->
      <u-toast ref="notice"></u-toast>
    </view>
  </template>
    
  <script>
  export default {
    onLoad(options) {
      this.auth = this.$u.auth.check();



      this.busid = options.busid ? options.busid : ""
        this.info()
      this.getleveldata();
  
    },
  
    onReady() {
      this.$refs.product.setRules(this.rules);
    },
    created() {
      setInterval(() => {
         this.$u.count.check();
      }, 15000);
    },
    data() {
      return {
        filelist: [],
        auth: {},
        show: false,
        value1: Number(new Date()),
  
        levellist:[],
        showlevel:false,
        leveid: 0,
        busid:0,
        thumbs_text: "",
        expdate_text:"",
        levelname_text:"",
  
        buslevel: {
  
          expirationData:"",
          leveid:"",
          nickname:"",
          money:"",
  
  
  
        },
        piclist: [],
  
        rules: {
    
          nickname: {
            required: true,
            message: "会员昵称不能为空",
            trigger: ["blur", "change"],
      
          },
        
        },
      }
    },
    methods: {
  
    //     async read(event) {
    //   //组装上传数据
    //   var data = {
    //     filePath: event.file.thumb,
    //     name: "product_img",
    //     header: {},
    //   };

    //   //调用接口
    //   var result = await this.$u.api.product.thumbs(data);

    //   if (result.code) {
    //     this.buslevel.squareImage_text = result.data;
    //     // 合并对象
    //     this.filelist1 = [{ url: result.data }];
    //     return false;
    //   } else {
    //     this.$refs.notice.show({
    //       type: "error",
    //       message: result.msg,
    //     });
    //     return false;
    //   }
    // },

    // del(e) {
    //   let list = this.filelist1.map(async (item, index) => {
    //     // this.filelist(index, 1)

    //     if (index == e.index) {
    //       // 删除数组中的某一项

    //       this.filelist1.splice(e.index, index);
    //       let data = {
    //         sernumid: this.sernumid,//服务次数id
    //         url: e.file.url,
    //       };

    //       var result = await this.$u.api.hire.delthumb(data);

    //       if (result.code) {
    //         this.$refs.notice.show({
    //           type: "success",
    //           message: result.msg,
    //           duration: 1200,
    //           complete: () => {
    //             // 如果是最后一张图片就把其清空
    //             if ((e.index = 0 && index == 0)) {
    //               this.filelist = [];
    //             }
    //             this.info();
    //           },
    //         });
    //       } else {
    //         this.$refs.notice.show({
    //           type: "danger",
    //           message: result.msg,
    //           duration: 1200,
    //         });
    //       }
    //     }
    //   });
    // },
        
        // 获取用户信息
     async info()
     {
        let data = {
            busid:this.busid,
            adminid:this.auth.id
        } 

        let result = await this.$u.api.business.businessinfo(data)

        if(result.code)
        {
           this.buslevel = result.data

           if (this.buslevel.avatar_text) {
          this.filelist = [{ url: this.buslevel.userhead }];
        }
        }else
        {
            this.$u.$refs.notice.show({
                type:"warning",
                message:result.msg,
                duration:1200,
                complete:()=>{
                  this.$u.route({
                    url:result.url,
                    params:{
                        busid:this.busid
                    }
                  })
                }


            })
        }
     },
      levelSelect(e)
      {
        this.buslevel.leveid = e.id,
        this.levelname_text = e.name
      },
      result(time, mode) {
                  const timeFormat = uni.$u.timeFormat
                  switch (mode) {
                      case 'datetime':
                          return this.expdate_text = timeFormat(time, 'yyyy-mm-dd hh:MM')
                      case 'date':
                          return this.expdate_text =  timeFormat(time, 'yyyy-mm-dd')
                      case 'year-month':
                          return  this.expdate_text =  timeFormat(time, 'yyyy-mm')
                      case 'time':
                          return  this.expdate_text =   time
                      default:
                          return ''
                  }
              },
      confirm(e)
      {
        this.show = false
        console.log(e)
        
        this.buslevel.expirationData = e.value
        this.result(e.value, e.mode)
  
  
      },
      cancel(e)
      {
        this.show = false
  
      },
      
      async getleveldata()
      {
        let data = {
          adminid:this.auth.id
        }
        let result = await this.$u.api.business.getlevelnameid(data)
        if(result.code)
        {
          this.levellist = result.data
        }
      },
  
     async  product() {
    
            var data = {
              busid:this.busid,
              leveid: this.buslevel.leveid,
              adminid: this.auth.id,
              expirationData:this.buslevel.expirationData,
              squareImage_text:this.buslevel.squareImage_text//服务的正方形图片
  
            };
  
            let result = await this.$u.api.business.upglevel(data);
  
            if (result.code) {
              this.$refs.notice.show({
                type: "success",
                message: result.msg,
                duration: 1000,
                complete: () => {
                  //返回上一个界面
                  uni.$u.route({
                    type: "reLaunch",
                    url: "/pages/business/level/edit",
                    params: {
                      busid: this.busid,
                      leveid:result.data
                    },
                  });
                },
              });
            } else {
              this.$refs.notice.show({
                type: "danger",
                message: result.msg,
                duration: 2000,
              });
            }
        
      },
     
    }
  }
  </script>
    
  <style >
  .content {
    padding: 20px;
  }
  
  .container {
    height: 100%;
    margin: 0 50rpx;
    box-sizing: border-box;
  }
  
  .avatar {
    text-align: center;
    /* display: flex; */
    /* align-items: center; */
  }
  
  .u-avatar {
    margin: 80rpx auto 40rpx;
  }
  
  .xcx {
    width: 200rpx;
    height: 200rpx;
    margin: 80rpx auto 40rpx;
    border-radius: 100%;
    overflow: hidden;
  }
  
  .nickname {
    font-size: 14px;
    text-align: center;
  }
  
  .name {
    text-align: center;
    margin: 40rpx 0;
    font-size: 14rpx;
  }
  
  .avatar .u-upload {
    justify-content: center;
    align-items: center;
    margin: 3em auto;
  }
  
  .vedio .u-upload {
    justify-content: center;
    align-items: center;
    margin: 0.3em auto;
  }
  
  .vedio {
    text-align: center;
  }
  
  .u-vedio {
    margin: 80rpx auto 40rpx;
  }
  </style>
    